package com.pur.zsms.foodmanage.food.web;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.FileUtill;
import com.pur.zsms.common.util.ImageResizer;
import com.pur.zsms.foodmanage.food.po.FoodMaterPo;
import com.pur.zsms.foodmanage.food.po.FoodTypePo;
import com.pur.zsms.foodmanage.food.service.IFoodService;
import com.pur.zsms.foodmanage.food.vo.FoodMaterVo;
import com.pur.zsms.foodmanage.food.vo.FoodTypeVo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.photomanage.po.PhotoPo;
import com.pur.zsms.photomanage.service.IPhotoService;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.shopmanage.po.ShopPo;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.vo.ShoppingCartCashierVo;

@Controller
@RequestMapping(value = "/food")
public class FoodControllor {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(FoodControllor.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "foodService")
	private IFoodService foodService;
	@Resource(name = "shopService")
	private IShopService shopService;
	@Resource(name = "photoService")
	private IPhotoService photoService;

	@RequestMapping(value = "/findFood.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findFood(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, FoodVo foodVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		foodVo.setUserId((int) userInfo.getUserId());
		this.pageList = foodService.listFood(pageController, foodVo);
		List<FoodVo> foodList = new ArrayList<FoodVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				FoodVo f = new FoodVo();
				f.setFoodId((Integer) item[0]);
				f.setFoodName((String) item[1]);
				BigDecimal value = new BigDecimal(0);
				value = (BigDecimal) (item[2]);
				if (value == null) {
					f.setFoodValue(value);
				} else {
					f.setFoodValue(value.setScale(2, BigDecimal.ROUND_HALF_UP));
				}

				f.setFoodType(item[3].toString());
				f.setFoodType1(Integer.valueOf(item[4].toString()));
				f.setFoodMtype((String) item[5]);
				f.setFoodMname((String) item[6]);
				f.setFoodMweight((String) item[7]);
				f.setFoodMater((String) item[8]);
				foodList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", foodList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/findFoodIndex.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findFoodIndex(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, FoodVo foodVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		foodVo.setUserId((int) userInfo.getUserId());
		this.pageList = foodService.listFoodIndex(pageController, foodVo);
		List<FoodVo> foodList = new ArrayList<FoodVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				FoodVo f = new FoodVo();
				f.setFoodId((Integer) item[0]);
				f.setFoodName((String) item[1]);
				f.setFoodType(item[2].toString());
				f.setFoodType1(Integer.valueOf(item[3].toString()));
				f.setFoodMtype((String) item[4]);
				f.setFoodMname((String) item[5]);
				f.setFoodMater("点击查看菜品材料");
				foodList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", foodList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 采购人员进入菜品材料详细页面
	 * */
	@RequestMapping(value = "/goFoodMaterDetailed.do", method = { RequestMethod.GET })
	public ModelAndView goFoodMaterDetailed(String foodId) {
		JSONObject json = new JSONObject();
		Map map = new HashMap();
		map.put("foodId", foodId);
		return new ModelAndView("/foodmanage/food/foodMaterDetailed", map);
	}

	@RequestMapping(value = "/listFoodMaterDetailed.do", method = { RequestMethod.POST })
	@ResponseBody
	public String listFoodMaterDetailed(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			FoodMaterVo foodMaterVo) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		this.pageList = foodService.listFoodMaterDetailed(pageController, foodMaterVo);
		List<FoodMaterVo> tFoodMaterVoList = new ArrayList<FoodMaterVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				FoodMaterVo f = new FoodMaterVo();
				f.setFmId((Integer) item[0]);
				f.setFoodId((Integer) item[1]);
				f.setFoodName((String) item[2]);
				f.setMaterId((Integer) item[3]);
				f.setMaterName((String) item[4]);
				f.setMaterWeight((BigDecimal) item[5]);

				tFoodMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", tFoodMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/saveFood.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveFood(FoodVo c) {
		JSONObject json = new JSONObject();
		try {

			c.setFoodValue(new BigDecimal(1));
			foodService.addFood(c);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/addFood.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addFood(FoodVo c) {
		JSONObject json = new JSONObject();
		try {

			String returnMessage = foodService.addFoodCheck(c);
			if (!returnMessage.equals("0000")) {
				json.put("success", false);
				json.put("msg", returnMessage);
				return json.toString();
			}
			c.setFoodValue(new BigDecimal(1));
			int footId = foodService.addFood(c);
			JSONArray array = JSONArray.fromObject(c.getFooddategridList());

			List<FoodMaterVo> foodMaterVoList = new ArrayList<FoodMaterVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				FoodMaterVo foodMaterVo = new FoodMaterVo();
				foodMaterVo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				foodMaterVo.setMaterWeight(new BigDecimal(object.get("materWeight").toString()));
				foodMaterVo.setFoodId(footId);
				foodMaterVoList.add(foodMaterVo);
			}
			foodService.addFoodMater(foodMaterVoList);

			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/preSaveFood.do", method = { RequestMethod.GET })
	public ModelAndView preSaveFood() {
		/*
		 * JSONObject json = new JSONObject(); List<RoleVo> roleList = null;
		 * 
		 * try { roleList = foodService.listAllRole(); //查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();
		// map.put("rolelist", roleList);
		return new ModelAndView("/foodmanage/food/addFood", map);
	}

	@RequestMapping(value = "/preUpdateFood.do", method = { RequestMethod.GET })
	public ModelAndView preUpdateFood(String foodId) {
		JSONObject json = new JSONObject();
		FoodVo foodVo = new FoodVo();
		try {

			foodVo = foodService.getFoodid(foodId); // 查询所有角色信息
			String checkBox = foodService.getFoodMaterId(foodId);
			foodVo.setCheckBox(checkBox);
		} catch (GenericException e) {
			e.printStackTrace();
		}
		Map map = new HashMap();
		map.put("foodVo", foodVo);
		return new ModelAndView("/foodmanage/food/addFoodMater", map);
	}

	@RequestMapping(value = "/updateFood.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateFood(FoodVo c) {

		JSONObject json = new JSONObject();
		try {
			// 对主表的修改
			foodService.updateFood(c);
			// 对附表的修改
			String fooddategridList = c.getFooddategridList();
			if (fooddategridList.substring(fooddategridList.length() - 1).equals(",")) {
				fooddategridList = fooddategridList.substring(0, fooddategridList.length() - 1);
			}
			JSONArray array = JSONArray.fromObject(fooddategridList);

			List<FoodMaterVo> foodMaterVoList = new ArrayList<FoodMaterVo>();
			for (int i = 0; i < array.size(); i++) {
				JSONObject object = JSONObject.fromObject(array.get(i));
				FoodMaterVo foodMaterVo = new FoodMaterVo();
				foodMaterVo.setMaterId(Integer.valueOf(object.get("materId").toString()));
				foodMaterVo.setMaterWeight(new BigDecimal(object.get("materWeight").toString()));
				foodMaterVo.setFoodId(c.getFoodId());
				foodMaterVoList.add(foodMaterVo);
			}
			foodService.addFoodMater(foodMaterVoList);

			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/deleteFood.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteFood(int foodId) {
		JSONObject json = new JSONObject();
		try {
			foodService.deleteFood(foodId);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}

	@RequestMapping(value = "/pulldownFoodType.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownFoodType() {
		JSONObject json = new JSONObject();
		String a = "[{\"foodType\":\"1\",\"foodTypename\":\"优\"},{\"foodType\":\"2\",\"foodTypename\":\"中\"},{\"foodType\":\"3\",\"foodTypename\":\"差\"}]";

		return a;
		// return json.toString();
	}

	@RequestMapping(value = "/pulldownFoodType1.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownFoodType1() {
		JSONObject json = new JSONObject();
		String a = "[{\"foodType1\":\"1\",\"foodTypename1\":\"牛肉类\"}," + "{\"foodType1\":\"2\",\"foodTypename1\":\"海鲜类\"},"
				+ "{\"foodType1\":\"3\",\"foodTypename1\":\"水发类\"}," + "{\"foodType1\":\"4\",\"foodTypename1\":\"猪肉类\"},"
				+ "{\"foodType1\":\"5\",\"foodTypename1\":\"浇头\"}," + "{\"foodType1\":\"6\",\"foodTypename1\":\"鸭肉类\"},"
				+ "{\"foodType1\":\"7\",\"foodTypename1\":\"鸡肉类\"}," + "{\"foodType1\":\"8\",\"foodTypename1\":\"鸡蛋类\"},"
				+ "{\"foodType1\":\"9\",\"foodTypename1\":\"鹌鹑类\"}," + "{\"foodType1\":\"10\",\"foodTypename1\":\"河鲜类\"},"
				+ "{\"foodType1\":\"11\",\"foodTypename1\":\"豆制品类\"}," + "{\"foodType1\":\"12\",\"foodTypename1\":\"根茎类\"},"
				+ "{\"foodType1\":\"13\",\"foodTypename1\":\"鸭蛋类\"}," + "{\"foodType1\":\"14\",\"foodTypename1\":\"干货类\"},"
				+ "{\"foodType1\":\"15\",\"foodTypename1\":\"绿叶菜\"}," + "{\"foodType1\":\"16\",\"foodTypename1\":\"瓜果类\"},"
				+ "{\"foodType1\":\"17\",\"foodTypename1\":\"叶菜\"}," + "{\"foodType1\":\"18\",\"foodTypename1\":\"其他类\"},"
				+ "{\"foodType1\":\"19\",\"foodTypename1\":\"猪骨类\"}," + "{\"foodType1\":\"20\",\"foodTypename1\":\"蛋类\"},"
				+ "{\"foodType1\":\"21\",\"foodTypename1\":\"酸辣\"}," + "{\"foodType1\":\"22\",\"foodTypename1\":\"咸菜类\"},"
				+ "{\"foodType1\":\"23\",\"foodTypename1\":\"鱼头类\"}," + "{\"foodType1\":\"24\",\"foodTypename1\":\"猪血\"}" + "]";
		return a;
		// return json.toString();
	}

	@RequestMapping(value = "/findFoodForCashier.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findFoodForCashier(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, FoodVo foodVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "999";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());

		foodVo.setShopId(shopId);
		this.pageList = foodService.listFoodForCashier(pageController, foodVo);
		List<FoodVo> foodList = new ArrayList<FoodVo>();
		List items = pageList.getItems();

		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				FoodVo f = new FoodVo();
				f.setFoodId((Integer) item[0]);
				f.setFoodName((String) item[1]);
				f.setFoodValue((BigDecimal) item[2]);
				foodList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", foodList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/preCashier.do", method = { RequestMethod.GET })
	public ModelAndView preCashier(HttpSession session) throws GenericException {
		/*
		 * JSONObject json = new JSONObject(); List<RoleVo> roleList = null;
		 * 
		 * try { roleList = foodService.listAllRole(); //查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		ShopPo shopPo = shopService.getShopPo(shopId);
		map.put("shopPo", shopPo);

		// 显示商品的list
		List<FoodTypeVo> listFoodTypeVo = new ArrayList<FoodTypeVo>();

		// 分类导航栏
		FoodTypeVo searchFoodTypeVo = new FoodTypeVo();
		searchFoodTypeVo.setShopId(shopId);
		List<FoodTypePo> listFoodTypePo = foodService.getFoodType(searchFoodTypeVo);

		if (listFoodTypePo != null && listFoodTypePo.size() > 0) {
			map.put("rowsFoodType", listFoodTypePo);

			for (FoodTypePo foodTypePo : listFoodTypePo) {
				// 显示商品用vo
				FoodTypeVo tFoodTypeVo = new FoodTypeVo();
				tFoodTypeVo.setFoodTypeId(foodTypePo.getFoodTypeId());
				tFoodTypeVo.setFoodTypeName(foodTypePo.getFoodTypeName());

				tFoodTypeVo.setHtmlFoodtypeId("foodType" + foodTypePo.getFoodTypeId());

				FoodVo foodVo = new FoodVo();// 检索条件用vo
				foodVo.setFoodType(String.valueOf(foodTypePo.getFoodTypeId()));
				foodVo.setShopId(shopId);
				List<Object[]> listObject = foodService.listFoodForCashier(foodVo);

				List<FoodVo> listFoodVo = new ArrayList<FoodVo>();

				if (listObject != null && listObject.size() > 0) {
					for (Object[] objects : listObject) {
						FoodVo foodVoForCashier = new FoodVo();
						foodVoForCashier.setFoodId(Integer.valueOf(objects[0].toString()));
						foodVoForCashier.setFoodName(objects[1].toString());
						foodVoForCashier.setFoodValue(new BigDecimal(objects[2].toString()));
						listFoodVo.add(foodVoForCashier);
					}
				}
				tFoodTypeVo.setFoodVolist(listFoodVo);
				tFoodTypeVo.setClassType(foodTypePo.getClassType());
				listFoodTypeVo.add(tFoodTypeVo);
			}
		}
		map.put("rows", listFoodTypeVo);

		PageController pageController = new PageController(1, 999);

		/*
		 * this.pageList = foodService.listFoodForCashier(pageController,
		 * foodVo); List<FoodVo> foodList = new ArrayList<FoodVo>(); List items
		 * = pageList.getItems();
		 * 
		 * if (items != null) { for (int i = 0; i < items.size(); i++) {
		 * Object[] item = (Object[]) items.get(i); FoodVo f = new FoodVo();
		 * f.setFoodId((Integer) item[0]); f.setFoodName((String) item[1]);
		 * f.setFoodValue((BigDecimal) item[2]); foodList.add(f); } }
		 */
		// map.put("rows", foodList);

		// map.put("rolelist", roleList);
		return new ModelAndView("/cashier/cashier", map);
	}

	@RequestMapping(value = "/preCashier2.do", method = { RequestMethod.GET })
	public ModelAndView preCashier2(HttpSession session) throws GenericException {
		/*
		 * JSONObject json = new JSONObject(); List<RoleVo> roleList = null;
		 * 
		 * try { roleList = foodService.listAllRole(); //查询所有角色信息 } catch
		 * (GenericException e) { e.printStackTrace(); }
		 */
		Map map = new HashMap();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		ShopPo shopPo = shopService.getShopPo(shopId);
		map.put("shopPo", shopPo);

		// 显示商品的list
		List<FoodTypeVo> listFoodTypeVo = new ArrayList<FoodTypeVo>();

		// 分类导航栏
		FoodTypeVo searchFoodTypeVo = new FoodTypeVo();
		searchFoodTypeVo.setShopId(shopId);
		List<FoodTypePo> listFoodTypePo = foodService.getFoodType(searchFoodTypeVo);

		if (listFoodTypePo != null && listFoodTypePo.size() > 0) {
			map.put("rowsFoodType", listFoodTypePo);

			for (FoodTypePo foodTypePo : listFoodTypePo) {
				// 显示商品用vo
				FoodTypeVo tFoodTypeVo = new FoodTypeVo();
				tFoodTypeVo.setFoodTypeId(foodTypePo.getFoodTypeId());
				tFoodTypeVo.setFoodTypeName(foodTypePo.getFoodTypeName());

				tFoodTypeVo.setHtmlFoodtypeId("foodType" + foodTypePo.getFoodTypeId());

				FoodVo foodVo = new FoodVo();// 检索条件用vo
				foodVo.setFoodType(String.valueOf(foodTypePo.getFoodTypeId()));
				foodVo.setShopId(shopId);
				List<Object[]> listObject = foodService.listFoodForCashier(foodVo);

				List<FoodVo> listFoodVo = new ArrayList<FoodVo>();

				if (listObject != null && listObject.size() > 0) {
					for (Object[] objects : listObject) {
						FoodVo foodVoForCashier = new FoodVo();
						foodVoForCashier.setFoodId(Integer.valueOf(objects[0].toString()));
						foodVoForCashier.setFoodName(objects[1].toString());
						foodVoForCashier.setFoodValue(new BigDecimal(objects[2].toString()));
						listFoodVo.add(foodVoForCashier);
					}
				}
				tFoodTypeVo.setFoodVolist(listFoodVo);
				tFoodTypeVo.setClassType(foodTypePo.getClassType());
				listFoodTypeVo.add(tFoodTypeVo);
			}
		}
		map.put("rows", listFoodTypeVo);

		PageController pageController = new PageController(1, 999);

		/*
		 * this.pageList = foodService.listFoodForCashier(pageController,
		 * foodVo); List<FoodVo> foodList = new ArrayList<FoodVo>(); List items
		 * = pageList.getItems();
		 * 
		 * if (items != null) { for (int i = 0; i < items.size(); i++) {
		 * Object[] item = (Object[]) items.get(i); FoodVo f = new FoodVo();
		 * f.setFoodId((Integer) item[0]); f.setFoodName((String) item[1]);
		 * f.setFoodValue((BigDecimal) item[2]); foodList.add(f); } }
		 */
		// map.put("rows", foodList);

		// map.put("rolelist", roleList);
		return new ModelAndView("/cashier/cashier2", map);
	}

	@RequestMapping(value = "/addFoodType.do", method = { RequestMethod.POST })
	public void preAddFoodType(FoodTypeVo c, HttpSession session) {
		UserPo lgUser = (UserPo) session.getAttribute("user");
		try {
			long shopId = shopService.getShopIdoByUserId(lgUser.getUserId());
			c.setShopId(shopId);
			foodService.addFoodType(c);
		} catch (GenericException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/addFoodForCashier.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addFoodForCashier(FoodVo c, HttpSession session) throws Exception {
		JSONObject json = new JSONObject();
		UserPo lgUser = (UserPo) session.getAttribute("user");
		int foodId = 0;
		try {
			long shopId = shopService.getShopIdoByUserId(lgUser.getUserId());
			c.setShopId(shopId);
			foodId = foodService.addFood(c);
			json.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
			return json.toString();
		}

		JSONObject json1 = new JSONObject();
		MultipartFile file = c.getFile();
		String temp = session.getServletContext().getRealPath(File.separator) + "temp"; // 临时目录
		File tempFile = new File(temp);
		if (!tempFile.exists()) {
			tempFile.mkdirs();
		}
		String filename1 = file.getOriginalFilename();
		String[] filename1Array = filename1.split("\\.");
		if (filename1Array.length != 2 || !filename1Array[1].equals("jpg")) {
			json1.put("success", false);
			json1.put("msg", "文件格式不对！请确保文件格式为jpg！");
			return json1.toString();
		}

		// 开始读取上传信息
		// int index = 0;
		if (file == null)
			return null;
		if (file.getSize() > 10000000) {
			throw new Exception("上传失败：文件大小不能超过10M");
		}

		// 设置路径
		String realPath = session.getServletContext().getRealPath("/");
		String photoRealPath = realPath + File.separator + "jsp" + File.separator + "ui" + File.separator + "dphoto" + File.separator;
		if (file.getSize() > 0) {
			try {
				FileUtill.saveFileFromInputStream(file.getInputStream(), photoRealPath, String.valueOf(foodId) + ".jpg");
			} catch (IOException e) {
				System.out.println(e.getMessage());
				return null;
			}
		} else {
			throw new Exception("上传失败：上传文件不能为空");
		}
		// 处理图片
		String dphotoRealPath = realPath + File.separator + "jsp" + File.separator + "ui" + File.separator + "dphoto" + File.separator + foodId
				+ ".jpg";
		String bigRealPath = realPath + File.separator + "jsp" + File.separator + "ui" + File.separator + "big" + File.separator + foodId + ".jpg";
		String midRealPath = realPath + File.separator + "jsp" + File.separator + "ui" + File.separator + "mid" + File.separator + foodId + ".jpg";
		String smallRealPath = realPath + File.separator + "jsp" + File.separator + "ui" + File.separator + "small" + File.separator + foodId
				+ ".jpg";
		ImageResizer.resizeImage(dphotoRealPath, bigRealPath, 1000, 800);
		ImageResizer.resizeImage(dphotoRealPath, midRealPath, 490, 390);
		ImageResizer.resizeImage(dphotoRealPath, smallRealPath, 78, 65);

		json1.put("success", true);

		return json.toString();
	}

	@RequestMapping(value = "/addShoppingCart.do", method = { RequestMethod.POST })
	@ResponseBody
	public String addShoppingCart(ShoppingCartCashierVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setShopId(shopId);

			boolean flag = foodService.addShoppingCartCashier(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", foodService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/updateShoppingCart.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateShoppingCart(ShoppingCartCashierVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setShopId(shopId);

			boolean flag = foodService.updateShoppingCartCashier(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", foodService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/findShoppingCartCashier.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findShoppingCartCashier(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			ShoppingCartCashierVo shoppingCartCashierVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		shoppingCartCashierVo.setShopId(shopId);
		this.pageList = foodService.findShoppingCartCashier(pageController, shoppingCartCashierVo);
		List<ShoppingCartCashierVo> shoppingCartCashierVoList = new ArrayList<ShoppingCartCashierVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				ShoppingCartCashierVo f = new ShoppingCartCashierVo();
				f.setCid((Integer) item[0]);
				f.setCartId((Integer) item[1]);
				f.setFoodId((Integer) item[2]);
				f.setFoodName((String) item[3]);
				f.setFoodValue((BigDecimal) item[4]);
				f.setFoodValueAll((BigDecimal) item[5]);
				f.setShopId(Long.valueOf(item[6].toString()));
				f.setNumber((BigDecimal) item[7]);
				shoppingCartCashierVoList.add(f);
			}
		}

		// 总额
		List<Object[]> listObject = foodService.getShoppingCartCasherTotalValue(shoppingCartCashierVo);
		if (listObject != null && listObject.size() > 0 && listObject.get(0) != null) {
			json.put("totalValue", listObject.get(0));
		} else {
			json.put("totalValue", 0);
		}

		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", shoppingCartCashierVoList);
		return json.toString();
	}

	@RequestMapping(value = "/deleteShoppingCart.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteShoppingCart(ShoppingCartCashierVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setShopId(shopId);
			foodService.deleteShoppingCartCashierPo(c);
			json.put("success", true);

		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/deleteCartOne.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteCartOne(ShoppingCartCashierVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setShopId(shopId);
			foodService.deleteShoppingCartOne(c);
			json.put("success", true);

		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/getCartOrderNumber.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getCartOrderNumber(HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			foodService.getCartOrderNumber(shopId);

			List<Object[]> listObject = foodService.getCartOrderNumber(shopId);
			if (listObject != null && listObject.size() > 0 && listObject.get(0) != null) {
				json.put("orderNumber", listObject.get(0));
			} else {
				json.put("orderNumber", 0);
			}
			json.put("success", true);

		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/getShoppingCartOrderTable.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getShoppingCartOrderTable(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			ShoppingCartCashierVo shoppingCartCashierVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		shoppingCartCashierVo.setShopId(shopId);
		this.pageList = foodService.getShoppingCartOrderTable(pageController, shoppingCartCashierVo);
		List<ShoppingCartCashierVo> shoppingCartCashierVoList = new ArrayList<ShoppingCartCashierVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				ShoppingCartCashierVo f = new ShoppingCartCashierVo();
				f.setCartId((Integer) item[0]);
				f.setFoodALL(item[1].toString());
				shoppingCartCashierVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", shoppingCartCashierVoList);
		return json.toString();
	}

	@RequestMapping(value = "/receiptCommit.do", method = { RequestMethod.POST })
	@ResponseBody
	public String receiptCommit(ShoppingCartCashierVo shoppingCartCashierVo, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			shoppingCartCashierVo.setShopId(shopId);
			shoppingCartCashierVo.setUserId(userInfo.getUserId());
			boolean flag = foodService.receiptCommit(shoppingCartCashierVo);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("msg", foodService.getMsg());
				json.put("success", false);
			}

		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	/***
	 * 收银界面检索后只显示商品 不显示分类
	 * 
	 * 
	 */
	@RequestMapping(value = "/searchFood.do", method = { RequestMethod.POST })
	@ResponseBody
	public String searchFood(FoodVo foodVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());

		foodVo.setShopId(shopId);
		List<Object[]> listObject = foodService.listFoodForCashier(foodVo);

		List<FoodVo> listFoodVo = new ArrayList<FoodVo>();

		if (listObject != null && listObject.size() > 0) {
			for (Object[] objects : listObject) {
				FoodVo foodVoForCashier = new FoodVo();
				foodVoForCashier.setFoodId(Integer.valueOf(objects[0].toString()));
				foodVoForCashier.setFoodName(objects[1].toString());
				foodVoForCashier.setFoodValue(new BigDecimal(objects[2].toString()));
				listFoodVo.add(foodVoForCashier);
			}
		}

		json.put("foodListForSearch", listFoodVo);
		json.put("success", true);

		PageController pageController = new PageController(1, 999);

		/*
		 * this.pageList = foodService.listFoodForCashier(pageController,
		 * foodVo); List<FoodVo> foodList = new ArrayList<FoodVo>(); List items
		 * = pageList.getItems();
		 * 
		 * if (items != null) { for (int i = 0; i < items.size(); i++) {
		 * Object[] item = (Object[]) items.get(i); FoodVo f = new FoodVo();
		 * f.setFoodId((Integer) item[0]); f.setFoodName((String) item[1]);
		 * f.setFoodValue((BigDecimal) item[2]); foodList.add(f); } }
		 */
		// map.put("rows", foodList);

		// map.put("rolelist", roleList);
		return json.toString();
	}

	/***
	 * 收银界面检索后显示分类和商品
	 * 
	 * 
	 */
	@RequestMapping(value = "/searchFoodDetail.do", method = { RequestMethod.POST })
	@ResponseBody
	public String searchFoodDetail(String foodName, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();

		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());

		// 显示商品的list
		List<FoodTypeVo> listFoodTypeVo = new ArrayList<FoodTypeVo>();

		// 分类导航栏
		FoodTypeVo searchFoodTypeVo = new FoodTypeVo();
		searchFoodTypeVo.setShopId(shopId);
		searchFoodTypeVo.setFoodName(foodName);
		List<FoodTypePo> listFoodTypePo = foodService.getFoodType(searchFoodTypeVo);

		if (listFoodTypePo != null && listFoodTypePo.size() > 0) {
			json.put("rowsFoodType", listFoodTypePo);

			for (FoodTypePo foodTypePo : listFoodTypePo) {
				// 显示商品用vo
				FoodTypeVo tFoodTypeVo = new FoodTypeVo();
				tFoodTypeVo.setFoodTypeId(foodTypePo.getFoodTypeId());
				tFoodTypeVo.setFoodTypeName(foodTypePo.getFoodTypeName());

				tFoodTypeVo.setHtmlFoodtypeId("foodType" + foodTypePo.getFoodTypeId());

				FoodVo foodVo = new FoodVo();// 检索条件用vo
				foodVo.setFoodType(String.valueOf(foodTypePo.getFoodTypeId()));
				foodVo.setShopId(shopId);
				foodVo.setFoodName(foodName);
				List<Object[]> listObject = foodService.listFoodForCashier(foodVo);

				List<FoodVo> listFoodVo = new ArrayList<FoodVo>();

				if (listObject != null && listObject.size() > 0) {
					for (Object[] objects : listObject) {
						FoodVo foodVoForCashier = new FoodVo();
						foodVoForCashier.setFoodId(Integer.valueOf(objects[0].toString()));
						foodVoForCashier.setFoodName(objects[1].toString());
						foodVoForCashier.setFoodValue(new BigDecimal(objects[2].toString()));
						listFoodVo.add(foodVoForCashier);
					}
				}
				tFoodTypeVo.setFoodVolist(listFoodVo);
				tFoodTypeVo.setClassType(foodTypePo.getClassType());
				listFoodTypeVo.add(tFoodTypeVo);
			}
		}
		json.put("rows", listFoodTypeVo);

		PageController pageController = new PageController(1, 999);
		json.put("success", true);

		return json.toString();
	}

	@RequestMapping(value = "/pulldownFoodTypeForShop.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownFoodTypeForShop(HttpSession session, String space) {
		JSONObject json = new JSONObject();
		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId;
		String a = null;
		try {
			shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			a = foodService.pulldownFoodTypeForShop(shopId, space);
		} catch (GenericException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return a;
	}
}
